package zwp.example.demo1;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import zwp.example.pojo.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@SpringBootTest
public class Demo2ListMap {

    @Test
    public void test1(){
        User user1 = new User();
        user1.setUserName("1");
        user1.setAge("10");
        User user2 = new User();
        user2.setUserName("2");
        user2.setAge("10");
        User user3 = new User();
        user3.setUserName("3");
        user3.setAge("20");
        User user4 = new User();
        user4.setUserName("4");
        user4.setAge("20");
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        Map<String, List<User>> userMap = list.stream().collect(Collectors.groupingBy(User::getAge));
//        Map<String,Object> map = new HashMap<String,Object>(8);
        for (Map.Entry<String, List<User>> stringListEntry : userMap.entrySet()) {
            System.out.println("Age: " + stringListEntry.getKey());
            System.out.println("Users: " + stringListEntry.getValue());
        }
    }

    /**
     * 报错测试
     */
    @Test
    public void test2(){
        Map<String,String> map = new HashMap<>(8);
        map.put("1","1");
        String s = map.get("2");
        System.out.println(s.getBytes());
    }
}
